use std::collections::HashMap;
use std::fmt;

#[derive(Debug, Clone)]
pub struct Metrics {
    pub data: HashMap<String, i64>
}

impl Metrics {
    pub fn new() -> Self {
        Self {
            data: HashMap::new()
        }
    }

    pub fn inc(&mut self, key: impl Into<String>) {
        let mut count = self.data.entry(key.into()).or_insert(0);
        *count += 1;
    }

    pub fn dec(&mut self, key: impl Into<String>) {
        let mut count = self.data.entry(key.into()).or_insert(0);
        *count -= 1;
    }

    pub fn snapshot(&self) -> HashMap<String, i64> {
        self.data.clone()
    }


}

impl fmt::Display for Metrics {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for entry in self.data.iter() {
            println!("{} : {} ", entry.0, entry.1);
            writeln!(f, "{}: {}", entry.0, entry.1)?;
        }
        Ok(())
    }
}
